View Javadoc

1   package net.sourceforge.simplegamenet.dice;
2   
3   import java.awt.*;
4   import java.awt.event.MouseEvent;
5   import java.awt.event.MouseListener;
6   import javax.swing.*;
7   
8   public class DicePlayDiceComponent extends JComponent implements MouseListener {
9   
10      private ImageIcon diceIcon;
11  
12      private int diceValue;
13      private int colorState = 0;
14  
15      private boolean diceSelected = false;
16      private boolean myTurn = false;
17  
18      private DicePlayPanel dicePlayPanel;
19  
20      public DicePlayDiceComponent(DicePlayPanel dicePlayPanel, int diceValue,
21                                   boolean diceSelected) {
22          this.dicePlayPanel = dicePlayPanel;
23          this.diceValue = diceValue;
24          this.diceSelected = diceSelected;
25          addMouseListener(this);
26          setMinimumSize(new Dimension(70, 70));
27          setPreferredSize(new Dimension(70, 70));
28          diceIcon = new ImageIcon(getClass().getResource("Dice.png"));
29      }
30  
31      public void paint(Graphics graphics) {
32          Dimension size = getSize();
33          graphics.drawImage(diceIcon.getImage(), 10, 10, size.width - 10, size.height - 10,
34                  diceValue * 300, 0, (diceValue * 300) + 300, 300, this);
35          switch (colorState) {
36              case 0:
37                  graphics.setColor(Color.LIGHT_GRAY);
38                  for (int i = 0; i < 5; i++) {
39                      graphics.drawRect(i, i, size.width - (2 * i) - 1,
40                              size.height - (2 * i) - 1);
41                  }
42                  break;
43              case 1:
44                  graphics.setColor(Color.RED);
45                  for (int i = 0; i < 5; i++) {
46                      graphics.drawRect(i, i, size.width - (2 * i) - 1,
47                              size.height - (2 * i) - 1);
48                  }
49                  break;
50              case 2:
51                  graphics.setColor(Color.GREEN);
52                  for (int i = 0; i < 5; i++) {
53                      graphics.drawRect(i, i, size.width - (2 * i) - 1,
54                              size.height - (2 * i) - 1);
55                  }
56                  break;
57              case 3:
58                  graphics.setColor(getBackground());
59                  for (int i = 0; i < 5; i++) {
60                      graphics.drawRect(i, i, size.width - (2 * i) - 1,
61                              size.height - (2 * i) - 1);
62                  }
63                  break;
64          }
65      }
66  
67      public void setDiceValue(int diceValue) {
68          this.diceValue = diceValue;
69          repaint();
70      }
71  
72      public int getDiceValue() {
73          return diceValue;
74      }
75  
76      /***
77       * Invoked when the mouse button has been clicked (pressed and released) on a component.
78       */
79      public void mouseClicked(MouseEvent mouseEvent) {
80      }
81  
82      /***
83       * Invoked when the mouse enters a component.
84       */
85      public void mouseEntered(MouseEvent mouseEvent) {
86          if (myTurn) {
87              if (!diceSelected) {
88                  colorState = 1;
89                  repaint();
90              }
91          }
92      }
93  
94      /***
95       * Invoked when the mouse exits a component.
96       */
97      public void mouseExited(MouseEvent mouseEvent) {
98          if (myTurn) {
99              if (!diceSelected) {
100                 colorState = 0;
101                 repaint();
102             }
103         }
104     }
105 
106     /***
107      * Invoked when a mouse button has been pressed on a component.
108      */
109     public void mousePressed(MouseEvent mouseEvent) {
110         if (myTurn) {
111             if (diceSelected) {
112                 colorState = 0;
113                 repaint();
114                 diceSelected = false;
115                 dicePlayPanel.diceClicked(this);
116             } else {
117                 colorState = 2;
118                 repaint();
119                 diceSelected = true;
120                 dicePlayPanel.diceClicked(this);
121             }
122         }
123     }
124 
125     /***
126      * Invoked when a mouse button has been released on a component.
127      */
128     public void mouseReleased(MouseEvent mouseEvent) {
129     }
130 
131     public void highlightDice() {
132         if (!myTurn) {
133             if (diceSelected == true) {
134                 colorState = 3;
135                 repaint();
136                 diceSelected = false;
137             } else {
138                 colorState = 2;
139                 repaint();
140                 diceSelected = true;
141             }
142         }
143     }
144 
145     public boolean getDiceSelected() {
146         return diceSelected;
147     }
148 
149     public void setMyTurn(boolean myTurn) {
150         this.myTurn = myTurn;
151         if (myTurn) {
152             colorState = 0;
153             repaint();
154         } else {
155             colorState = 3;
156             repaint();
157         }
158     }
159 
160     public void resetDice() {
161         colorState = 0;
162         diceSelected = false;
163     }
164 }